<?php
namespace App\Service\ApiQd;

use App\Service\BaseService as Service;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BaseService extends Service
{
    //基础服务

    //通过手机号获取uid
    public function getUidByPhone($phone)
    {
        if (!$phone) {
            return 0;
        }
        $res = (array) $this->dbwbConn($this->dbDistributorUser)
            ->where('phone', $phone)
            ->first();
        if (empty($res)) {
            return 0;
        }
        return $res['id'];
    }

    //通过token获取uid
    public function getUidByToken($token)
    {
        if (!$token) {
            return 0;
        }
        $res = (array) $this->dbwbConn($this->dbDistributorUser)
            ->where('token', $token)
            ->first();
        if (empty($res)) {
            return 0;
        }
        return $res['id'];
    }

    //通过token获取用户信息
    public function getUserByToken($token)
    {
        if (!$token) {
            return 0;
        }
        $res = (array) $this->dbwbConn($this->dbDistributorUser)
            ->where('token', $token)
            ->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }

    /**
     * 插入接口统计
     * @param string $phone
     */
    public function addApiCount($token)
    {
        $uid = $this->getUidByToken($token);
//        $uri = $_SERVER['REQUEST_URI'];
//        $resOne = (array) DB::table($this->dbDoc)->where('uri', $uri)->first();
//        if (!empty($resOne)) {
//            DB::table($this->dbDoc)->where('id', $resOne['id'])
//                ->update(['count' => $resOne['count'] + 1]);
//            DB::table($this->dbDocCount)->insertGetId([
//                'uid' => $uid,
//                'doc_id' => $resOne['id'],
//                'addtime' => toDate(),
//            ]);
//        }
        return $uid;
    }

    //获取配置
    public function getConfig($key = '')
    {
        $db = $this->dbwbConn($this->dbQdConfig);
        $res = $db->get();
        if (!$key) {
            $arys = [];
            foreach ($res as $v) {
                $v = (array) $v;
                $arys[$v['key']] = $v['value'];
            }
            return $this->toback(0, '', $arys);
        }
        $res = (array) $db->where('key', $key)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有参数');
        }
        return $this->toback(0, '', $res['value']);
    }
}